package com.ftg.learn.controller;

import cn.hutool.captcha.CaptchaUtil;
import cn.hutool.captcha.LineCaptcha;
import cn.hutool.core.lang.tree.Tree;
import com.ftg.learn.dto.LoginDto;
import com.ftg.learn.service.UserService;
import com.ftg.learn.util.JwtUtils;
import com.ftg.learn.util.R;
import io.jsonwebtoken.Claims;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.net.http.HttpResponse;
import java.util.List;

/**
 * @author 布衣
 */
@RestController
@RequestMapping("/authCenter")
@Tag(name = "登录接口")
public class LoginController {

    private String code;

    @Resource
    private UserService userService;

    @Operation(summary = "登陆方法")
    @PostMapping("/login")
    public R<String> login(@RequestBody LoginDto loginDto) {
        return userService.login(loginDto,code);
    }


    @Operation(summary = "验证码")
    @GetMapping("/captcha")
    public void captcha(HttpServletResponse response) throws IOException {
        LineCaptcha lineCaptcha = CaptchaUtil.createLineCaptcha(200, 100);
        code = lineCaptcha.getCode();
        System.out.println(code);
        lineCaptcha.write(response.getOutputStream());
    }

    @Operation(summary = "获取当前用户权限")
    @GetMapping("/getCurUserPerById")
    public R<List<Tree<Long>>> getCurUserPerById(
           @RequestHeader("token") String token
    ){

        return userService.getCurUserPerById(token);
    }
}
